package com.zeroup.auto.assists.step.unify;

import static kotlinx.coroutines.DelayKt.delay;

import android.graphics.Path;
import android.graphics.Rect;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ScreenUtils;
import com.ven.assists.Assists;
import com.ven.assists.AssistsWindowManager;
import com.ven.assists.stepper.Step;
import com.ven.assists.stepper.StepCollector;
import com.zeroup.auto.assists.DouYinBaseStep;
import com.zeroup.auto.assists.kt.MyContinuation;
import com.zeroup.auto.assists.next.StepBeanData;
import com.zeroup.auto.assists.step.unify.bean.FlowValue;
import com.zeroup.auto.assists.step.unify.bean.LikeValue;
import com.zeroup.auto.utils.AssistsUtils;
import com.zeroup.auto.utils.RandomUtile;

import java.util.List;

public class LikeStep extends DouYinBaseStep {
    @Override
    public void onImpl(@NonNull StepCollector stepCollector) {
        stepCollector.next(1,false,(i,c)->{
            Object serviceData = AssistsUtils.getServiceData(i.getData());
            if(serviceData==null){
                return AssistsUtils.nextStep( i.getData());
            }
            if(serviceData instanceof StepBeanData){
                StepBeanData stepBeanData = (StepBeanData)serviceData;
                LikeValue likeValue = JSON.parseObject(stepBeanData.getDataValue(),LikeValue.class);
                int second = RandomUtile.rangInt(likeValue.getExeMinSecond(),likeValue.getExeMaxSecond());
                int num = RandomUtile.rangInt(likeValue.getSecondMinNum(),likeValue.getSecondMaxNum());
                int x = ScreenUtils.getAppScreenWidth() / 2;
                int y = ScreenUtils.getAppScreenHeight() / 2;
                MyContinuation myContinuation = new MyContinuation();
                Path mPath=new Path();
                mPath.moveTo(x, y);
                mPath.lineTo(x, y);
                do {
                    do {
                        this.runMain(b->{
                            AssistsWindowManager.INSTANCE.switchNotTouchableAll();
                            Assists.gesture(mPath,0,1,myContinuation);
                            Assists.gesture(mPath,0,1,myContinuation);
                            AssistsWindowManager.INSTANCE.switchTouchableAll();
                            return null;
                        },myContinuation);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        Log.d("LikeStep===>",x+":" + y+ ",执行了双击事件");
                        num--;
                    }while (num>0);
                    second--;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }while (second>0);

            }
            return AssistsUtils.nextStep(i.getData());
        });
    }
}
